<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--table标题插槽-->
      <template #tableTitle>
        <a-button type="primary" :preIcon="IconEnum.ADD" @click="handleAdd">{{ t('action.create') }}</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="handleBatchDelete">
                <Icon :icon="IconEnum.DELETE"></Icon>
                {{ t('action.delete') }}
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >{{ t('action.batchOpt') }}
            <Icon icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏插槽-->
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
      <template v-slot:bodyCell="{ column,text }">
        <!--超链接处理,先写个demo,后面可能用到.参考网址:https://blog.csdn.net/u014458962/article/details/106758063-->
        <template v-if="column.dataIndex === 'valueStr'">
            <div v-html="text"></div>
        </template>
    </template>
    </BasicTable>
    <MultiSpecModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="goods-multiSpec" setup>
import { ref, unref } from 'vue';
import { useI18n } from '@/hooks/web/useI18n';
import { IconEnum } from '@/enums/appEnum';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { getPage, deleteById, batchDelete } from './multiSpec.api';
import { columns, searchFormSchema } from './multiSpec.data';
import MultiSpecModal from './MultiSpecModal.vue';
import { useListPage } from '/@/hooks/system/useListPage';
import { useGo } from '/@/hooks/web/usePage';
const [registerModal, { openModal }] = useModal();
const { t } = useI18n();
const go = useGo();
// 列表页面公共参数、方法
const { tableContext } = useListPage({
  designScope: 'multiSpec-template',
  tableProps: {
    title: '商品多规格列表',
    api: getPage,
    columns: columns,
    useSearchForm: true,
    size: 'small', //紧凑型表格
    formConfig: {
      labelWidth: 60,
      autoSubmitOnEnter: true,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 180,
    },
    showIndexColumn: true,
  },
});

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: t('action.edit'),
      onClick: handleEdit.bind(null, record),
    },
    {
      label: t('action.delete'),
      popConfirm: {
        title: t('common.delMessage'),
        placement: 'left',
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
  });
}

/**
 * 编辑事件
 */
function handleEdit(record) {
  openModal(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteById({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function handleBatchDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, reload);
}
</script>

